package com.eastmelon.blog.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eastmelon.basic.model.Pager;
import com.eastmelon.basic.model.SystemContext;
import com.eastmelon.blog.entity.Tags;
import com.eastmelon.blog.service.IndexService;
import com.eastmelon.blog.service.TagsService;

@Controller
@RequestMapping("/back/tags")
public class TagsController {
	@Resource(name = "tagsService")
	private TagsService tagsService;
	@Resource(name = "indexService")
	private IndexService indexService;

	@RequestMapping("/tagsInfor")
	public String tagsInfor(Model model) {
		SystemContext.setPageSize(10);
		Pager<Tags> pt = tagsService.findPage();
		model.addAttribute("pt", pt);
		return "back/tags/tagsInfor";
	}

	@RequestMapping("addTags")
	public String addTags(Tags tags) {
		Integer pos = tagsService.getMaxPos();
		if (pos != null) {
			tags.setPos(pos + 1);
		}
		else {
			tags.setPos(1);
		}
		tagsService.save(tags);
		indexService.createNav();
		return "redirect:/back/tags/tagsInfor.do";
	}

	@RequestMapping("updateTags")
	public String updateTags(Tags tags) {
		tagsService.update(tags);
		indexService.createNav();
		return "redirect:/back/tags/tagsInfor.do";
	}
	
	@RequestMapping("deleteTag")
	public String delTag(int id){
		tagsService.deleteById(id);
		indexService.createNav();
		return "redirect:/back/tags/tagsInfor.do";
	}
	
	@RequestMapping("deleteTags")
	public String deTags(String  ids){
		String[] idsS=ids.split(",");
		tagsService.deleteByIds(idsS);
		indexService.createNav();
		return "redirect:/back/tags/tagsInfor.do";
	}

}
